import React from 'react'
import { useRoutes } from "react-router-dom"
import routes from "../router/router"
/**
 * 动态路由组件，负责动态生成页面中的路由信息
 * @returns 
 */

function DynamicRouter() {
    const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}")
    if (Object.keys(userInfo) != 0) {
        //userInfo = {role:{name:"超集管理员",menus:[]}}
        const { role: { menus = [] } } = userInfo
        //menus = ["/home/user","/home/shop"]
        //menus = ["user"]
        const userRoutes = menus.map(item => {
            return item.substr(6)
        })
        const index = routes.findIndex(item => item.path == "/home")
        let tempArray = [...routes[index].children]
        //"/home/" router.js index=true
        tempArray = tempArray.filter(item => {
            if (userRoutes.includes(item.path) || item.index) {
                return true
            }
        })
        routes[index].children = tempArray

    }
    //这个地方筛选出你最终路由
    const element = useRoutes(routes)
    return element
}

export default DynamicRouter